package main

// go语言实现二分法查找
import (
	"fmt"
)

func main() {
	var a = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	index := find(a, 99)
	if -1 == index {
		fmt.Printf("sorry ! not found!")
		return
	}
	fmt.Printf("find %d from array of a, it's index is %d", 8, index)
}

func find(arr []int, data int) int {
	var low, height = 0, len(arr)
	for {
		if low >= height {
			break
		}
		mid := low + (height-low)/2

		if data == arr[mid] {
			return mid
		} else if data < arr[mid] {
			height = mid - 1
		} else {
			low = mid + 1
		}
	}
	return -1
}
